package com.jinshan.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinshan.edu.entity.EduCourse;
import com.jinshan.edu.entity.EduCourseDescription;
import com.jinshan.edu.mapper.EduCourseMapper;
import com.jinshan.edu.service.*;
import com.jinshan.request.CourseInfoVO;
import com.jinshan.response.EduCourseInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author Jinshan
 * @since 2020-06-26
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Autowired
    private EduCourseDescriptionService courseDescriptionService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private EduSectionService sectionService;

    @Autowired
    private EduCourseDescriptionService descriptionService;

    @Override
    public boolean saveCourse(CourseInfoVO courseInfoVO) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVO, eduCourse);
        int rows = baseMapper.insert(eduCourse);
        if (rows > 0) {
            EduCourseDescription description = new EduCourseDescription();
            description.setDescription(courseInfoVO.getDescription());
            description.setId(eduCourse.getId());
            boolean flag = courseDescriptionService.save(description);
            if (flag) return true;
        }
        return false;
    }

    @Override
    public CourseInfoVO getCourseById(String courseId) {
        CourseInfoVO courseInfoVO = new CourseInfoVO();
        EduCourse eduCourse = baseMapper.selectById(courseId);
        BeanUtils.copyProperties(eduCourse, courseInfoVO);
        EduCourseDescription description = courseDescriptionService.getById(courseId);
        if (description != null) {
            courseInfoVO.setDescription(description.getDescription());
        }
        return courseInfoVO;
    }

    @Override
    public List<EduCourse> getAllCourse() {
        List<EduCourse> courses = baseMapper.selectList(null);
        return courses;
    }

    @Override
    public void getCourseByCondition(Page<EduCourse> page, String title, String status) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if (!"".equals(title)) {
            wrapper.like("title", title);
        }
        if (!"".equals(status)) {
            wrapper.eq("status", status);
        }
        baseMapper.selectPage(page, wrapper);
    }

    // 更新课程和课程简介
    @Override
    public boolean updateCourse(CourseInfoVO courseInfoVO) {
        EduCourse course = new EduCourse();
        BeanUtils.copyProperties(courseInfoVO, course);
        int rows = baseMapper.updateById(course);
        if (rows > 0) {
            EduCourseDescription description = new EduCourseDescription();
            description.setDescription(courseInfoVO.getDescription());
            description.setId(courseInfoVO.getId());
            return courseDescriptionService.updateById(description);
        }
        return false;
    }

    @Override
    public EduCourseInfoVO getCourseInfoById(String courseId) {
        EduCourseInfoVO courseInfoVO = baseMapper.getCourseInfoById(courseId);
        return courseInfoVO;
    }

    @Override
    public void updateCourseStatus(String courseId) {
        EduCourse course = new EduCourse();
        course.setId(courseId);
        course.setStatus("Normal");
        baseMapper.updateById(course);
    }

    @Override
    public boolean deleteCourseById(String courseId) {
        // 删除chapter
        chapterService.deleteChapterByCourseId(courseId);
        // 删除section
        sectionService.deleteSectionByCourseId(courseId);
        // 删除description
        descriptionService.removeById(courseId);
        // 删除section的同时, 需要删除section关联的视频,
        baseMapper.deleteById(courseId);
        return true;
    }
}
